package algo.combinatorics

import scala.collection.mutable.ListBuffer

/*
Problem Statement

At the annual meeting of Board of Directors of Acme Inc, every one starts shaking hands with everyone else in the room. Given the fact that any two persons shake hand exactly once, Can you tell the total count of handshakes?

Input Format 
The first line contains the number of test cases T, T lines follow. 
Each line then contains an integer N, the total number of Board of Directors of Acme.

Output Format

Print the number of handshakes for each test-case in a new line.

Constraints

1 <= T <= 1000 
0 < N < 106
*/

/*
 Say there are 5 gentlemen. 1st gentleman can handshake with 4 others. 
 2nd gentlemen has already shaken hands with 1st one so he has 3 more.
 so continuing we get 4 + 3 + 2 + 1 = 10 which is reduced to ---> n*(n-1)/2 ( Sigma(n-1) )
 */

object HandShake {

  def main(args: Array[String]) {
    val count = readLine
    val tests = ListBuffer[Int]()
    for (i <- 0 until count.toInt) {
      val num = readLine
      tests += num.toInt
    }
    tests.foreach({ num =>
      val res = (num * (num - 1))/2
      println(res)
    })
  }
  
  

  private def factorial(n: Int): BigInt = {
    factorial2(n, 1)
  }

  private def factorial2(n: Int, result: BigInt): BigInt = {
    if (n <= 0) result else factorial2(n - 1, n * result);
  }

  private def nCr(n: Int, r: Int): BigInt = {
    factorial(n) / (factorial(r) * factorial(n - r))
  }
}